/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.ui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.view.View;
import android.widget.ListAdapter;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.network.MarketAPI;
import com.mol.market.common.util.Utils;
import com.mol.market.common.vo.TagsInfo;
import com.mol.market.common.widget.CategoryTagView;
import com.mol.market.common.widget.CategoryTagView.TagViewListener;
import com.mol.market.demo.widget.BaseFragmentActivity.KeyStore;
import com.mol.market.demo.widget.CommonAdapter;
import com.mol.market.demo.widget.CommonAdapter.ViewBinder;
import com.mol.market.demo.widget.ListFragmentWrapper;

/**
 * 游戏/应用 精选标签
 * @author Chaos	
 * @Description TODO
 * @date 2012-12-26 
 */
public class TagFragment extends ListFragmentWrapper
                implements TagViewListener{
    
    @Override
    protected ListAdapter initAdapter() {
        CommonAdapter adapter = new CommonAdapter(this,
                null,
                R.layout.activity_category_tags_list,
                new String[]{
                    Constants.KEY_GROUP_NAME,
                    Constants.KEY_TAGS},
                new int[]{
                    R.id.tv_title,
                    R.id.tag_item});
        
        adapter.setViewBinder(new ViewBinder() {
            
            @Override
            public boolean setViewValue(View view, Object data, int position, Object textRepresentation) {
                if(view.getId() == R.id.tag_item){
                    CategoryTagView category = (CategoryTagView)view;
                    category.setTagsViewOnClickListener(TagFragment.this);
                    category.changeTags((List<TagsInfo>) textRepresentation);
                    return true;
                }
                return false;
            }
        });
        
        return adapter;
    }

    @Override
    protected void doLoad() {
        MarketAPI.getCategoryTags(getApplicationContext(), this);
    }
    
    @Override
    public void onSuccess(int method, Object obj) {
        super.onSuccess(method, obj);
        
        if (getAdapter() != null)
            ((CommonAdapter) getAdapter()).addData((ArrayList<HashMap<String, Object>>) obj);
    }

    @Override
    public void onTagsViewClick(View v, TagsInfo tag) {
        if(tag == null){
            //unlucky
            return ;
        }
        
        Utils.jumpToPage(getActivity(), ThirdFragmentActivity.class, false, 
                new String[]{
                    KeyStore.KEY_ENTRY,
                    KeyStore.KEY_ARG,
                    KeyStore.KEY_TITLE
                }, 
                new Serializable[]{
                    KeyStore.TARGET_CATEGORY_TAG_LIST,
                    tag.getTagId(),
                    tag.getTagName()
                });
    }

}
